package cn.net.autocode.platform.excel.handle;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson2.JSONObject;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.Map;

public class ExcelHandle {


    public static Object valueTransform(JSONObject cell, JSONObject titleCell, Map<String,JSONObject> dictMap){
        String dataType = titleCell.getString("dataType");
        Object cellValue = cell.get("value");
        if(cellValue == null){
            return null;
        }

        if("datetime".equals(dataType)){
            if(cellValue instanceof String dd){
                String format = titleCell.getString("format");
                if(!dd.trim().isEmpty()){
                    return DateUtil.parse(dd.trim(), format);
                }else{
                    return  null;
                }
            }else if(cellValue instanceof Date){
                return cellValue;
            }else{
                throw new RuntimeException(cell.getString("address")+"处的数据格式不正确！");
            }
        }else if("number".equals(dataType)){
            if(cellValue instanceof String dd){
                if(!dd.trim().isEmpty()){
                    if(NumberUtil.isNumber(dd.trim())){
                        return dd.trim();
                    }else{
                        throw new RuntimeException(cell.getString("address")+"处的数据格式不正确！");
                    }
                }else{
                    return null;
                }
            }else if(cellValue instanceof Double){
                return cellValue;
            }else{
                throw new RuntimeException(cell.getString("address")+"处的数据格式不正确！");
            }
        }else if("text".equals(dataType)){
            if(cellValue instanceof Double dd){
                if(dd == Math.floor(dd)){
                    return String.valueOf(dd.longValue());
                }else{
                    DecimalFormat df = new DecimalFormat("#.#######");
                    return df.format(dd);
                }
            }else if(cellValue instanceof Date celldate){
                return DateUtil.format(celldate,"yyyy-MM-dd HH:mm:ss");
            }else{
                return cellValue.toString().trim();
            }
        }else if("email".equals(dataType)){
            String email = cellValue.toString().trim();
            if (!Validator.isEmail(email,true)){
                throw new RuntimeException(cell.getString("address")+"处的邮箱地址无效！");
            }
            return email;
        }else if("phone".equals(dataType)){
            String phone;
            if(cellValue instanceof Double dd){
                phone = String.valueOf(dd.longValue());
            }else{
                phone = cellValue.toString().trim();
            }
            if (!Validator.isMobile(phone)){
                throw new RuntimeException(cell.getString("address")+"处的手机号无效！");
            }
            return phone;

        }else if("idcard".equals(dataType)){
            String idcard = cellValue.toString().trim();
            if (!Validator.isCitizenId(idcard)){
                throw new RuntimeException(cell.getString("address")+"处的身份证号码无效！");
            }
            return idcard;
        }else if("dict".equals(dataType)){
            String dictLabel = cellValue.toString().trim();
            if (dictLabel.isBlank()){
                return null;
            }else{
                String dictCode = titleCell.getString("dictCode").trim();
                String dictValue = dictMap.get(dictCode).getString(dictLabel);
                if(dictValue == null){
                    throw new RuntimeException(dictLabel+"对应的字典值没有在"+dictCode+"中维护！");
                }
                return dictValue;
            }
        }
        else{
            return cellValue;
        }
    }
}
